/* LoginForm.java 
* Created on Nov 29, 2012 
*@Author: Mr.Viet 
*@Phone: 01683430426 
*@Email: phamvanvietcdhd@gmail.com
*/ 
package com.session6; 

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

public class LoginForm extends JFrame implements ItemListener{
	private JTextField txtName,txtPass;
	private JLabel lblName,lblPass,lblLanguage;
	private JComboBox cboLang;
	private JButton btnLogin;
	public JFrame parent;
	private Vector<Language> data;
	public LoginForm(){
		this.data = new Vector<Language>();
		this.data.addElement(new Language("vn", "VN", "Vietnam"));
		this.data.addElement(new Language("en", "US", "English"));
	}
	public void createForm(){
		this.setLocationRelativeTo(parent);
		this.setLayout(new MigLayout());
		this.txtName = new JTextField(20);
		this.txtPass = new JTextField(20);
		
		this.btnLogin = new JButton("Login");
		
		this.lblName = new JLabel("Username");
		this.lblPass = new JLabel("Password");
		this.lblLanguage = new JLabel("Language");
		
		this.cboLang = new JComboBox(this.data);
		
		this.getContentPane().add(this.lblName);
		this.getContentPane().add(this.txtName,"wrap");
		
		this.getContentPane().add(this.lblPass);
		this.getContentPane().add(this.txtPass,"wrap");
		
		this.getContentPane().add(this.lblLanguage);
		this.getContentPane().add(this.cboLang,"wrap");
		
		this.getContentPane().add(new JLabel());
		this.getContentPane().add(this.btnLogin);
		//Dang Ki Su Kien
		this.cboLang.addItemListener(this);
		
	}
	public void showForm(){
		this.setTitle("Demo");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		this.setSize(400,300);
	}
	public void changeLang(String lang,String country){
		//Step 1: Create Locale instance
		Locale locale = new Locale(lang,country);
		//Step 2: Create RessourBundle instance (basename)
		ResourceBundle bundle = ResourceBundle.getBundle("com.session6.ngonngu",locale);
		//Step 3: Change label by language
		this.btnLogin.setText(bundle.getString("btnlogin"));
		this.lblName.setText(bundle.getString("lblname"));
		this.lblPass.setText(bundle.getString("lblpass"));
		this.lblLanguage.setText(bundle.getString("lbllanguage"));
	}
	public static void main(String[] args) {
		LoginForm d = new LoginForm();
		d.createForm();
		d.changeLang("vn", "VN");//Dat Mac Dinh No Hien Thi Nen La Ngon Ngu Gi?
		d.showForm();
	}
	@Override
	public void itemStateChanged(ItemEvent e) {
		if(e.getSource() == this.cboLang){
			Language lang = (Language)this.cboLang.getSelectedItem();
			this.changeLang(lang.getLang(), lang.getCountry());
		}
	}

}